﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YiYiTong.Rocket.Model;
using YiYiTong.Rocket.Helper;

namespace YiYiTong.Rocket.Business
{
    /// <summary>
    /// 微信授权登录相关
    /// </summary>
    public class WechatOauthBiz
    {
        /// <summary>
        /// 获取access_token
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public static Tuple<bool, WxTokenModel> GetAccessToken(string code)
        {
            string appid = ConfigHelper.AppSetting<string>("wx_oauth_appid");
            string secret = ConfigHelper.AppSetting<string>("wx_oauth_secret");
            string url = ConfigHelper.AppSetting<string>("wx_oauth_url");
            string data = string.Format("appid={0}&secret={1}&code={2}&grant_type=authorization_code", appid, secret, code);

            string result = HttpHelper.HttpGet(url, data);
            var dicRet = JsonHelper.DeserializeLower(result);
            if (dicRet.ContainsKey("errcode"))
            {
                LogHelper.Error("微信授权登录,获取access_token失败：" + dicRet.GetValue("errmsg"));
                return new Tuple<bool, WxTokenModel>(false, null);
            }
            else
            {
                //成功获取access_token
                WxTokenModel model = JsonHelper.Deserialize<WxTokenModel>(result);
                return new Tuple<bool, WxTokenModel>(true, model);
            }
        }

        /// <summary>
        /// 刷新access_token
        /// </summary>
        /// <returns></returns>
        public static Tuple<bool, WxTokenModel> RefreshToken(string refreshToken)
        {
            string appid = ConfigHelper.AppSetting<string>("wx_oauth_appid");
            string refreshUrl = ConfigHelper.AppSetting<string>("wx_oauth_refreshurl");
            string data = string.Format("appid={0}&grant_type=refresh_token&refresh_token={1}", appid, refreshToken);

            string result = HttpHelper.HttpGet(refreshUrl, data);
            var dicRet = JsonHelper.DeserializeLower(result);
            if (dicRet.ContainsKey("errcode"))
            {
                LogHelper.Error("微信授权登录,刷新access_token失败：" + dicRet.GetValue("errmsg"));
                return new Tuple<bool, WxTokenModel>(false, null);
            }
            else
            {
                WxTokenModel model = JsonHelper.Deserialize<WxTokenModel>(result);
                return new Tuple<bool, WxTokenModel>(true, model);
            }
        }

        /// <summary>
        /// 获取微信用户个人信息
        /// </summary>
        public static Tuple<bool, WxUserModel> GetUser(string accessToken, string openid)
        {
            string url = ConfigHelper.AppSetting<string>("wx_user_url");
            string data = string.Format("access_token={0}&openid={1}&lang=zh_CN", accessToken, openid);

            string result = HttpHelper.HttpGet(url, data);
            var dicRet = JsonHelper.DeserializeLower(result);
            if (dicRet.ContainsKey("errcode"))
            {
                LogHelper.Error("获取微信用户个人信息失败：" + dicRet.GetValue("errmsg"));
                return new Tuple<bool, WxUserModel>(false, null);
            }
            else
            {
                LogHelper.Error("获取微信用户个人信息成功==" + result);
                WxUserModel model = JsonHelper.Deserialize<WxUserModel>(result);
                return new Tuple<bool, WxUserModel>(true, model);
            }
        }
    }
}
